<template>
  <div class="line-today-wrap">
    <base-charts v-if="list.length > 0" id="lineTodayDomId" style="margin-top: 10px; height: 100%;" :option="todayLineOption" @inited="todayChartInited"></base-charts>
    <div v-else style="margin-top: 10px; height: 100%; display: flex; justify-content: center; align-items: center;" class="no-data">
      暂无数据
    </div>
  </div>
</template>

<script>
import {getDataUploadTrend} from '@/api/h3yun'
import BaseCharts from '@/components/charts/base-charts.vue'
import * as echarts from 'echarts'

export default {
  components: {
    BaseCharts
  },
  name: 'line-today',
  props: ['areaCode'],
  data() {
    return {
      list: [],
      timer: null,
      chartInstance: null
    }
  },
  mounted() {
    // 设置定时器，每分钟刷新一次数据
    this.timer = setInterval(() => {
      this.initData()
    }, 60000)
  },
  beforeDestroy() {
    // 组件销毁前清除定时器
    if (this.timer) {
      clearInterval(this.timer)
      this.timer = null
    }
  },
  watch: {
    // 监听数据变化，更新图表
    list: {
      handler(newVal) {
        if (this.chartInstance && newVal.length > 0) {
          this.updateChart()
        }
      },
      deep: true
    },
    areaCode: {
      handler(newVal) {
        this.initData()
      },
      deep: true,
      immediate: true
    }
  },
  computed: {
    todayLineOption() {
      return {
        backgroundColor: 'transparent',
        title: {
         text: '%',
          top: '0%',
          left: '3.5%',
          textStyle: {
            fontSize: 12,
            color: '#ffffff'
          }
        },
        tooltip: {
          show: true,
          trigger: 'item',
          axisPointer: {
            type: 'cross',
            label: {
              backgroundColor: '#6a7985'
            },
            lineStyle: {
              color: 'rgba(110,212,255,0.6)',
              width: 1
            },
            crossStyle: {
              color: 'rgba(110,212,255,0.6)'
            }
          },
          formatter: function(params) {
            return `时间点：${params.name}<br/>上报率：${params.value}%`;
          },
          backgroundColor: 'rgba(25,72,124,0.9)',
          borderColor: 'rgba(110,212,255,0.8)',
          borderWidth: 1,
          padding: [8, 12],
          extraCssText: 'box-shadow: 0 0 8px rgba(0, 0, 0, 0.3);',
          textStyle: {
            fontSize: 13,
            color: '#fff',
          }
        },
        grid: {
          left: '5%',
          right: '5%',
          top: '20%',
          bottom: '5%',
          containLabel: true
        },
        xAxis : {
          type: 'category',
          boundaryGap: false,
          data: this.list.map(it => it.name),
          axisLabel: {
            color: '#fff',
            fontSize: 11,
            interval: 'auto'
          },
          axisLine: {
            lineStyle: {
              color: 'rgba(110,212,255,0.3)'
            }
          }
        },
        yAxis: {
          type: 'value',
          splitNumber: 4,
          axisLabel: {
            color: '#fff',
            fontSize: 11
          },
          axisTick: {
            show: false,
          },
          axisLine: {
            lineStyle: {
              color: 'rgba(110,212,255,0.3)'
            }
          },
          splitLine: {
            lineStyle: {
              type: 'dashed',
              color: 'rgba(54,83,125,1)'
            }
          }
        },
        series: [
          {
            name: '报警数量',
            data: this.list.map(it => it.value),
            type: 'line',
            barWidth: 8,
            symbol: 'circle',
            showSymbol: true,
            symbolSize: 5,
            lineStyle: {
              width: 2,
              shadowColor: 'rgba(55,187,251,0.3)',
              shadowBlur: 10
            },
            emphasis: {
              focus: 'series',
              itemStyle: {
                borderColor: '#fff',
                borderWidth: 2,
                shadowColor: 'rgba(55,187,251,0.8)',
                shadowBlur: 10,
                color: '#37BBFB'
              }
            },
            itemStyle: {
              normal: {
                color: new echarts.graphic.LinearGradient(0, 0, 1, 0, [
                  { offset: 0, color: '#3977E6' },
                  { offset: 1, color: '#37BBFB' },
                ]),
                borderColor: 'rgba(255,255,255,0.8)',
                borderWidth: 1,
                shadowColor: 'rgba(55,187,251,0.5)',
                shadowBlur: 5
              }
            },
            smooth: true,
            areaStyle: {
              color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                { offset: 0, color: 'rgba(57, 119, 230, 0.3)' },
                { offset: 1, color: 'rgba(57, 119, 230, 0.1)' }
              ])
            }
          }
        ],
        animation: true,
        animationDuration: 1000
      }
    }
  },
  methods: {
    async initData() {
      try {
        let res = await getDataUploadTrend(this.areaCode)
        this.list = res.data.map(s => ({
          name: s.name,
          value: s.value
        }))
      } catch (error) {
        console.error('获取数据失败:', error)
      }
    },
    todayChartInited(chart) {
      // 保存图表实例，以便后续可能的操作
      this.chartInstance = chart
      // 强制显示tooltip
      if (chart && this.list.length > 0) {
        this.updateChart()
      }
    },
    updateChart() {
      // 更新图表数据
      this.chartInstance.setOption({
        xAxis: {
          data: this.list.map(it => it.name)
        },
        series: [{
          data: this.list.map(it => it.value)
        }]
      })
      // 确保tooltip正常工作
      this.chartInstance.dispatchAction({
        type: 'showTip',
        seriesIndex: 0,
        dataIndex: 0
      })
      // 移除初始tooltip显示（延迟执行）
      setTimeout(() => {
        this.chartInstance.dispatchAction({
          type: 'hideTip'
        })
      }, 3000)
    }
  }
}
</script>

<style scoped lang="scss">
.line-today-wrap {
  margin-top: 10px;
  width: 100%;
  height: 100%;
  position: relative;
  overflow: hidden;
  
  &::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    height: 1px;
    background: linear-gradient(to right, transparent, rgba(110,212,255,0.5), transparent);
    opacity: 0.5;
  }
}
.no-data {
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: rgba(255, 255, 255, 0.6);
  font-size: 14px;
}
</style>
